package com.wzq.blog.back.controller;

import com.wzq.blog.back.bean.User;
import com.wzq.blog.back.service.UserService;
import com.wzq.blog.base.bean.ResultVo;
import com.wzq.blog.base.exception.BlogException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 校验用户登录信息
     * @param user
     * @param code
     * @param session
     * @return
     */
    @RequestMapping("/back/user/login")
    @ResponseBody
    public ResultVo login(User user, String code, HttpSession session){
        ResultVo resultVo = new ResultVo();
        //从 session 域中获取验证码
        String rightCode = (String) session.getAttribute("code");
        try{
            user = userService.login(user,code,rightCode);
            resultVo.setOk(true);
            //把登录用户存放到 session 中
            session.setAttribute("user",user);
        }catch (BlogException e){
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

    /**
     * 用户登录成功跳转到后台首页
     * @return
     */
    @RequestMapping("/workbench/index")
    public String index(){
        return "/workbench/index";
    }

    /**
     * 退出登录功能
     * @param session
     * @return
     */
    @RequestMapping("/user/loginOut")
    public String loginOut(HttpSession session){
        //清除 session 中的用户
        session.removeAttribute("user");
        //重定向到登录页面
        return "redirect:/";
    }

    /**
     * 异步校验用户输入的原始密码是否正确
     * @param oldPwd
     * @param session
     * @return
     */
    @RequestMapping("/user/verifyOldPwd")
    @ResponseBody
    public ResultVo verifyOldPwd(String oldPwd, HttpSession session){
        ResultVo resultVo = new ResultVo();
        try {
            User user = (User) session.getAttribute("user");
            userService.verifyOldPwd(oldPwd,user);
            resultVo.setOk(true);
        } catch (BlogException e) {
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

    /**
     * 异步修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/user/updateUser")
    @ResponseBody
    public ResultVo updateUser(User user){
        ResultVo resultVo = new ResultVo();
        try {
            userService.updateUser(user);
            resultVo.setOk(true);
            resultVo.setMess("修改用户信息成功！");
        } catch (BlogException e) {
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }
}
